<?php

namespace app\user\logic;

use app\common\exception\LogicException;
use app\model\Machine;

class MachineLogic
{
    /**
     * @Description 机器绑定
     * @author: zj
     * @param int $factoryId
     * @param string $machineCode
     * @time 2024/03/27 12:05:33
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function bind(int $factoryId, string $machineCode)
    {
        $machineModel = new Machine(); 
        $machineInfo = $machineModel->field('id,factory_id')
            ->where('machine_code', $machineCode)
            ->where('delete_time', $machineModel::IS_DELETE_NO)
            ->find();

        //不存在
        if (!$machineInfo) {
            $machineModel->insert([
                'factory_id' => $factoryId,
                'machine_code' => $machineCode,
                'create_time' => request()->time(),
                'update_time' => request()->time(),
            ]);

            return true;
        }

        if ($machineInfo['factory_id'] == $factoryId) {
            throw new LogicException('机器编号已绑定过');
        }
        if ($machineInfo['factory_id'] != 0) {
            throw new LogicException('机器编号已被其他猪厂绑定');
        }

        $machineModel->where('id', $machineInfo['id'])
            ->update([
                'factory_id' => $factoryId,
                'update_time' => request()->time(),
            ]);

        return true; 
    }

    /**
     * @Description 列表
     * @author: zj
     * @param int $factoryId
     * @param array $param
     * @time 2024/03/27 12:05:18
     * @return mixed
     */
    public function getList(int $factoryId, array $param)
    {
        $machineModel = new Machine();
        $query = $machineModel->alias('m')
            ->leftJoin('machine_version mv', 'm.now_version_id=mv.id')
            ->leftJoin('machine_version mv2', 'm.target_version_id=mv2.id')
            ->field('m.id,m.machine_code,m.create_time,mv.version_name as now_version_name,mv2.version_name as target_version_name')
            ->where('m.factory_id', $factoryId)
            ->where('m.delete_time', $machineModel::IS_DELETE_NO);

        if ($param['machine_code']) {
            $query->where('m.machine_code', $param['machine_code']);
        }

        return $query->order('m.id', 'desc')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * @Description 详情
     * @author: zj
     * @param int $factoryId
     * @param int $machineId
     * @time 2024/03/27 12:05:03
     * @return mixed
     */
    public function getDetail(int $factoryId, int $machineId)
    {
        $machineModel = new Machine();
        $machineInfo = $machineModel->alias('m')
            ->leftJoin('machine_version mv', 'm.now_version_id=mv.id')
            ->leftJoin('machine_version mv2', 'm.target_version_id=mv2.id')
            ->field('m.id,m.machine_code,m.create_time,mv.version_name as now_version_name,mv2.version_name as target_version_name')
            ->where('m.id', $machineId)
            ->where('m.factory_id', $factoryId)
            ->where('m.delete_time', $machineModel::IS_DELETE_NO)
            ->find();

        if (!$machineInfo) {
            throw new LogicException('machine_id不存在');
        }

        return $machineInfo->toArray();
    }

    /**
     * @Description 解绑
     * @author: zj
     * @param int $factoryId
     * @param int $machineId
     * @time 2024/03/27 12:04:56
     * @return true
     */
    public function unbind(int $factoryId, int $machineId)
    {
        $machineModel = new Machine();
        $exist = $machineModel->checkExistById($factoryId, $machineId);
        if (!$exist) {
            throw new LogicException('machine_id不存在');
        }

        $machineModel->where('id', $machineId)
            ->update([
                'factory_id' => 0,
                'update_time' => request()->time()
            ]);

        return true;
    }
}